Background
This is a basic tutorial on how to load shapefiles and geopoints to a leaflet map.
There is a lot more detailed information here [https://rstudio.github.io/leaflet/](https://rstudio.github.io/leaflet/)
The example is hopefully useful as it uses UK postcode and district shapefiles, which are commonly useful for a variety of purposes.
UK Postcode data can be found here
https://www.freemaptools.com/map-tools.htm
UK district shapefiles are available here
https://www.ordnancesurvey.co.uk/opendatadownload/products.html
Libraries
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.4.1 ✔ purrr 1.0.1
✔ tibble 3.2.1 ✔ dplyr 1.1.0
✔ tidyr 1.3.0 ✔ stringr 1.5.0
✔ readr 2.1.4 ✔ forcats 1.0.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
Linking to GEOS 3.10.2, GDAL 3.4.2, PROJ 8.2.1; sf_use_s2() is TRUE
library (knitr)
library (leaflet)
Data
Create a folder to house data
if (! dir.exists ("data/ukpostcodes" ))(dir.create ("data/ukpostcodes" ))
system ("rm -rf data/ukpostcodes/*" )
Download a list of UK postcodes and gps locations
download.file (url = "https://data.freemaptools.com/download/full-uk-postcodes/ukpostcodes.zip" ,destfile = paste ("data/ukpostcodes/ukpostcodes.zip" ))
system ("unzip data/ukpostcodes/ukpostcodes.zip -d data/ukpostcodes/" )
Download a list of UK district shapefiles
# set a timeout proportional to the size of the dataset and the speed of the connection
options (timeout= 300 )
download.file (url = "https://api.os.uk/downloads/v1/products/BoundaryLine/downloads?area=GB&format=ESRI%C2%AE+Shapefile&redirect" ,destfile = paste ("data/ukpostcodes/bdline_essh_gb.zip" ))
system ("unzip data/ukpostcodes/bdline_essh_gb.zip -d data/ukpostcodes/" )
Read Postcode data
ukpostcodes <- read_csv ("data/ukpostcodes/ukpostcodes.csv" )
Rows: 1794534 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): postcode
dbl (3): id, latitude, longitude
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Make a small sample of all postcodes
sample<- ukpostcodes[sample (1 : nrow (ukpostcodes),size = 100 ),] %>%
mutate (
latitude = as.numeric (latitude),
longitude = as.numeric (longitude)
)
head (sample)
# A tibble: 6 × 4
id postcode latitude longitude
<dbl> <chr> <dbl> <dbl>
1 1764911 TQ11 0FH 50.5 -3.78
2 2285418 NR17 1YY 52.5 1.00
3 1543104 BN41 1LN 50.8 -0.215
4 1454712 CH41 6NZ 53.4 -3.03
5 963438 KY2 5XN 56.1 -3.26
6 596151 PA13 4EX 55.9 -4.63
Read in shapefiles to a map
map = read_sf ("data/ukpostcodes/Data/GB/county_region.dbf" )
mapproj <- st_transform (map, st_crs ("+proj=longlat +init=epsg:4326 +ellps=WGS84 +datum=WGS84 +no_defs" ))
Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
deprecated. It might return a CRS with a non-EPSG compliant axis order.
leaflet (mapproj) %>%
addTiles () %>%
addMeasure (primaryLengthUnit= "kilometers" , secondaryLengthUnit= "meters" ) %>%
addScaleBar (options = c (imperial = FALSE )) %>%
addPolygons (color = "green" ,label = mapproj$ NAME) %>%
addCircleMarkers (lng = sample$ longitude,lat = sample$ latitude,label = sample$ postcode,radius = 0.3 )